package com.horse.cloud.gateway.filter;

import com.alibaba.fastjson.JSON;
import com.horse.cloud.framework.general.enums.ResponseCodeEnums;
import com.horse.cloud.framework.general.response.Response;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpResponse;
import reactor.core.publisher.Mono;

/**
 * @create: Created by intelliJ IDEA18.1.3
 * @author: bing.Pan
 * @e-mail: 15923508369@163.com
 * @gmdate: 09/11/2018　18:47　星期五 (dd/MM/YYYY HH:mm)
 * @sidesc:
 */
public class GatewayResultAdapter {

    public static Mono<Void> jsonAdapter(ServerHttpResponse response, ResponseCodeEnums responseCodeEnums){
        HttpHeaders httpHeaders = response.getHeaders();
        httpHeaders.add("Content-Type", "application/json; charset=UTF-8");
        httpHeaders.add("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0");

        String result = JSON.toJSONString(new Response<>(responseCodeEnums));
        DataBuffer bodyDataBuffer = response.bufferFactory().wrap(result.getBytes());
        return response.writeWith(Mono.just(bodyDataBuffer));
    }

    public static Mono<Void> htmlAdapter(ServerHttpResponse response, HttpStatus httpStatus){
        response.setStatusCode(httpStatus);
        return response.setComplete();
    }
}
